package com.play.ballen.home.adapter

import android.os.Build
import androidx.annotation.RequiresApi
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.play.ballen.R
import com.play.ballen.home.entity.ArticleInfo
import com.play.ballen.home.entity.BottleListData
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.text.SimpleDateFormat
class BottleListAdapter(data: MutableList<BottleListData>) :
    BaseQuickAdapter<BottleListData, BaseViewHolder>(R.layout.item_bottle, data) {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun convert(holder: BaseViewHolder, item: BottleListData) {
        holder.setText(R.id.tvContent, item.content)
        holder.setText(R.id.tv_num, "评论数 "+item.comment_count.toString())
        holder.setText(R.id.red_count, item.unread_count.toString())
        holder.setText(R.id.tv_time, convertDateTime(item.post_time))
    }
}

@RequiresApi(Build.VERSION_CODES.O)
fun convertDateTime(inputDateTime: String): String {
    val formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd EEEE", Locale.SIMPLIFIED_CHINESE)

    val offsetDateTime = OffsetDateTime.parse(inputDateTime)
    val zoneId = ZoneId.of("Asia/Shanghai")
    val zonedDateTime = offsetDateTime.atZoneSameInstant(zoneId)

    return zonedDateTime.format(formatter)
}
